package geektime.io.nio2.demo;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * 在当前文件系统下获取路径
 *
 */
public class PathInfoDemo {
    public static void main(String[] args) {
        FileSystem fsDefault = FileSystems.getDefault();
        Path path = fsDefault.getPath("a", "b", "c", "d");
        System.out.println("********打印出每层的路径名*******");
        System.out.println(path); // a\b\c\d
        System.out.printf("FileSystem: %s\n", path.getFileSystem()); // sun.nio.fs.WindowsFileSystem
        System.out.printf("File name: %s\n", path.getFileName()); // d
        System.out.printf("Parent: %s\n", path.getParent()); // a\b\c
        System.out.printf("Root: %s\n", path.getRoot()); // null
        System.out.printf("SubPath[0,2): %s\n", path.subpath(0, 2)); // a\b
        System.out.println("********打印出每层的路径名*******");
        for (int i = 0; i < path.getNameCount(); i++) {
            System.out.println(path.getName(i));
        }

        Path normalPath = fsDefault.getPath("a", "./b/c/d/../../e");
        System.out.printf("Normalized file name: %s\n", normalPath.normalize());

        Path relativizePath = normalPath.normalize().relativize(path.getParent());
        System.out.printf("Relativized file name: %s\n", relativizePath);
    }
}
